package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService facilityService;

    @Autowired
    private RoomLabelService labelService;

    @Autowired
    private  RoomPaymentTypeService paymentTypeService;

    @Autowired
    private  RoomLeaseTermService leaseTermService;



    @Autowired
   private  RoomInfoMapper infoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private  AttrValueMapper attrValueMapper;


    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private  PaymentTypeMapper paymentTypeMapper;

    @Autowired LeaseTermMapper leaseTermMapper;


    /**
     *  步骤:
     *      1.先判断id号
     *          没有id 新增操作
     *          有id   修改操作
     *              先删除中间表数据  之后新增
     */
    @Override
    @Transactional
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        if(roomSubmitVo.getId() !=null) {
            //1.删除房间图片信息
            Long roomId = roomSubmitVo.getId();
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId, roomId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //2.房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //3.房间配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomId);
            facilityService.remove(roomFacilityLambdaQueryWrapper);

            //4.房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelServiceLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
            labelService.remove(roomLabelServiceLambdaQueryWrapper);

            //5.支付类型
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
            paymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //6.租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
            leaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        }
            //先应该新增房间 才能回显房间号
            this.saveOrUpdate(roomSubmitVo);
            long roomId = roomSubmitVo.getId();

            //1.新增图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
           if (!CollectionUtils.isEmpty(graphVoList)){
               List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            //将用户的数据vo 转化为可以入库的info对象
               graphVoList.forEach(graphVo -> {
                   GraphInfo info = new GraphInfo();
                   info.setName(graphVo.getName());
                   info.setUrl(graphVo.getUrl());
                   info.setItemType(ItemType.ROOM);
                   info.setItemId(roomId);
                   graphInfoList.add(info);
               });
                graphInfoService.saveBatch(graphInfoList);

        }
        //2.新增配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
            if(!CollectionUtils.isEmpty(facilityInfoIds)){
                    List<RoomFacility> facilityList = new ArrayList<>(facilityInfoIds.size());
                facilityInfoIds.forEach(facilityInfoId ->{
                    RoomFacility info = new RoomFacility();
                    info.setFacilityId(facilityInfoId)
                        .setRoomId(roomId);
                  facilityList.add(info);
                });
                //入库操作
                facilityService.saveBatch(facilityList);
            }

           //3.新增标签信息
        List<Long> labelIds = roomSubmitVo.getLabelInfoIds();
             if (!CollectionUtils.isEmpty(labelIds)){
             List<RoomLabel> labelList = new ArrayList<>(labelIds.size());
                   labelIds.forEach(labelId -> {
                       RoomLabel roomLabel = RoomLabel.builder()
                               .labelId(labelId)
                               .roomId(roomId)
                               .build();
                       labelList.add(roomLabel);
                   });
                 //入库操作
                   labelService.saveBatch(labelList);

             }

            //4.新增房间属性值
        List<Long>  ValueIds = roomSubmitVo.getAttrValueIds();
             if (!CollectionUtils.isEmpty(ValueIds)){
             List<RoomAttrValue> attrValues = new ArrayList<>(ValueIds.size());
                 ValueIds.forEach(attrValueID -> {
                         RoomAttrValue roomAttrValue =RoomAttrValue.builder()
                                 .roomId(roomId)
                                 .attrValueId(attrValueID)
                                 .build();

                         attrValues.add(roomAttrValue);
                 });
                 //入库操作
                 roomAttrValueService.saveBatch(attrValues);
             }

             //5.新增支付类型
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
             if (!CollectionUtils.isEmpty(paymentTypeIds)){
             List<RoomPaymentType> roomPaymentTypes = new ArrayList<>(paymentTypeIds.size());
                 paymentTypeIds.forEach(paymentTypeId ->{
                     RoomPaymentType paymentType = RoomPaymentType.builder()
                             .paymentTypeId(paymentTypeId)
                             .roomId(roomId)
                             .build();
                     roomPaymentTypes.add(paymentType);
                 });
                 //入库操作
                 paymentTypeService.saveBatch(roomPaymentTypes);
             }

             //6.新增租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
                if (!CollectionUtils.isEmpty(leaseTermIds)){
             List<RoomLeaseTerm> leaseTerms = new ArrayList<>(leaseTermIds.size());
                leaseTermIds.forEach(termId ->{
                    RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                            .leaseTermId(termId)
                            .roomId(roomId)
                            .build();
                    leaseTerms.add(roomLeaseTerm);
                });
      leaseTermService.saveBatch(leaseTerms);
                }
    }

    /**
     * 1.查询room_info表进行分页查询
     * 2.每个房间:
     *           room_info lease_agreement  agreement
     *          leaseEndDate  租期到期时间
     *          isCheckIn    true 已入住   false未入住   2,5,7已入住
     *          apartmentInfo 房间属于的公寓信息.
     */
    @Override
    public IPage<RoomItemVo> pageRoom(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return infoMapper.pageRoom(page,queryVo);
    }

    @Override
    @Transactional
    public RoomDetailVo getRoomDetailById(Long id) {

      //1.查询RoomInfo
        RoomInfo roomInfo = infoMapper.selectById(id);

        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo =  apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //3.查询graphInfoList
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(id);

        //4.查询attrValueList
        List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);

        //5.查询facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        //6.查询labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        //7.查询paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        //8.查询leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);



        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(graphVoList);
        adminRoomDetailVo.setAttrValueVoList(attrvalueVoList);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setLabelInfoList(labelInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
        adminRoomDetailVo.setLeaseTermList(leaseTermList);



        return adminRoomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除RoomInfo
        this.removeById(id);



        //2.删除graphInfoList
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //3.删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
        attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrQueryWrapper);

        //4.删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        facilityService.remove(facilityQueryWrapper);

        //5.删除labelInfoList
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        labelService.remove(labelQueryWrapper);

        //6.删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
        paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        paymentTypeService.remove(paymentQueryWrapper);

        //7.删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
        termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        leaseTermService.remove(termQueryWrapper);
    }
}





